<?php
/**
 * 公用函数库文件
 * @copyright (c)2011-2012 php300.com
 * @license   dxtoo.cn
 * @author    lnbalife@126.com
 * @version   1.0beta
 */

/**
 * 设置和读取配置信息
 * @param unknown_type $name
 * @param unknown_type $value
 */
function mc($name=null, $value=null) {
    static $_config = array();
    if (empty($name)) return $_config;
        
    if (is_string($name)) {
        if (!strpos($name, '.')) {
            $name = strtolower($name);
            if (is_null($value)) return isset($_config[$name]) ? $_config[$name] : null;
            $_config[$name] = $value;
            return;
        }
        //支持多维数组
        $name = explode('.', $name);
        
        if (is_null($value)){
        	$namekey = isset($_config[strtolower($name[0])])?$_config[strtolower($name[0])]:null;
	        unset($name[0]);
	        if(!empty($name))foreach($name as $v){
	        	$namekey = isset($namekey[strtolower($v)])?$namekey[strtolower($v)]:null;
	        }
	        return $namekey;
        }else{
			$s = '$_config[$name[0]]';
        	for($i=1;$i<count($name);$i++){
				$s.= '[$name['.$i.']]';
        	}
        	eval("$s = \$value;");
        }
        return;
    }
    
    if (is_array($name))
        return $_config = array_merge($_config, array_change_key_case($name));
    return null;
}

/**
 * 返回经addslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function cxaddslashes($string){
	if(!is_array($string)) return addslashes($string);
	foreach($string as $key => $val) $string[$key] = cxaddslashes($val);
	return $string;
}

/**
 * 读取和处理文件内容
 * @param unknown_type $filename
 * @param unknown_type $runtime
 */
function compile($filename, $runtime=false) {
    $content = file_get_contents($filename);
    if (true === $runtime)
        $content = preg_replace('/\/\/\[RUNTIME\](.*?)\/\/\[\/RUNTIME\]/s', '', $content);
    $content = substr(trim($content), 5);
    if ('?>' == substr($content, -2))
        $content = substr($content, 0, -2);
    return $content;
}

/**
 * 去除内容中的空格和回车
 * @param unknown_type $content
 */
function strip_whitespace($content) {
    $stripStr = '';
    $tokens = token_get_all($content);
    $last_space = false;
    for ($i = 0, $j = count($tokens); $i < $j; $i++) {
        if (is_string($tokens[$i])) {
            $last_space = false;
            $stripStr .= $tokens[$i];
        } else {
            switch ($tokens[$i][0]) {
                case T_COMMENT:
                case T_DOC_COMMENT:
                    break;
                case T_WHITESPACE:
                    if (!$last_space) {
                        $stripStr .= ' ';
                        $last_space = true;
                    }
                    break;
                default:
                    $last_space = false;
                    $stripStr .= $tokens[$i][1];
            }
        }
    }
    return $stripStr;
}

/**
 * 循环建立目录
 * @param unknown_type $dir
 * @param unknown_type $mode
 */
function mk_dir($dir, $mode = 0777) {
    if (is_dir($dir) || @mkdir($dir, $mode))
        return true;
    if (!mk_dir(dirname($dir), $mode))
        return false;
    return @mkdir($dir, $mode);
}

/**
 * 显示异常信息
 * @param unknown_type $msg
 * @param unknown_type $redircet
 */
function exceptionMsg($msg,$redircet=''){
	header("Content-Type:text/html; charset=utf-8");
	$html = '<div style=\'font-weight:bold;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>'.$msg.'</div>';
	if($redircet){
		echo $html;
		exit;
	}
	exit($html);
}
?>